<?php

namespace App\Http\Controllers\Dash;

use App\Http\Controllers\Controller;
use App\Model\Associado;
use App\Model\Contato;
use Illuminate\Http\Request;

class AssociadoController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:associado-list', ['only' => ['index']]);
        $this->middleware('permission:associado-delete', ['only' => ['delete']]);
        $this->middleware('permission:associado-edit', ['only' => ['edit', 'update']]);
        $this->middleware('permission:associado-create', ['only' => ['create', 'store']]);
    }

    public function index()
    {
        $data = Associado::all();
        return view('dash.associado.index', compact('data'));
    }

    public function create()
    {
        return view('dash.associado.create');
    }

    public function store(Request $request)
    {
        $assoc = new Associado();
        $assoc->name = $request->name;
        $assoc->description = $request->description;
        $assoc->link = $request->link;
        $assoc->save();
        return redirect()->route('dash.associado')->with('success', $assoc);
    }

    public function edit($id)
    {
        $data = Associado::find($id);
        return view('dash.associado.edit', compact('data', 'id'));
    }

    public function update($id, Request $request)
    {
        $assoc = Associado::find($id);
        $assoc->name = $request->name;
        $assoc->decription = $request->description;
        $assoc->link = $request->link;
        $assoc->save();
        return redirect()->route('dash.associado')->with('success', $assoc);
    }

    public function delete($id)
    {
        $assoc = Associado::find($id)->delete();
        return redirect()->route('dash.associado')->with('success', $assoc);
    }
}
